package shinesend.com.cn.valaanimtordemo;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;

/**
 * Created by MyPC on 2016/11/9.
 * 数值动画;
 */
public class ValueActvity extends Activity {

    private ImageView ball;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_value_animator);
        ball = (ImageView) findViewById(R.id.ball);
    }

    public void pure(View veiw){
        //需要运动的路程;
        final float width = getResources().getDisplayMetrics().widthPixels-ball.getWidth();
        //设置波动范围;
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f,1f);
        //设置周期
        valueAnimator.setDuration(3000);
        //设置变速
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        //设置监听,可以监听运动更新的轨迹
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //给球设置x位移动画;
                    ball.setTranslationX(((float)animation.getAnimatedValue())*width);
            }
        });
        //开始动画
        valueAnimator.start();

    }


    public  void cancel(View view){
        ball.clearAnimation();
    }

}
